﻿using System;
using System.Collections.Generic;
using Co9320.NamaAlert.Engine;
using Co9320.NamaAlert.Model;
using helper = KeyWordSearch.DebugHelper;
using normalize = KeyWordSearch.Normalization;
using parser = KeyWordSearch.TreeParser;
using process = KeyWordSearch.ProcessTree;
using diag = System.Diagnostics;
using System.Linq.Expressions;
using System.Linq;
using System.Text.RegularExpressions;


namespace TestBench
{
	class Program
	{
		const string Local = "http://seedcreate.dip.jp/rss/20091007/{0}.xml";
		const string Nico = "http://live.nicovideo.jp/recent/rss?tab=&sort=&p={0}";

		private static List<LiveProgram> ProgramList_=new List<LiveProgram>();

		private static process::RootNode root_;

		static void Main(string[] args)
		{
			MainDemo();
			
		}
		


		private static void MainDemo()
		{
			//Build filter
			parser::TokenStream str = new KeyWordSearch.TreeParser.TokenStream(
				"(contain(\"プログラミング\",title|description,kana)||contain(\"プログラム\",title|description,both)||contain(\"開発\",title|description,kana))"+
			"&& !(contain(\"FSP\",title|description,alpha)||contain(\"ダイエット\",title|description,kana)||contain(\"gdgd\",title|description,alpha))"
				);


			while (str.MoveNext())
			{
				Console.WriteLine(str.Current);
				Console.WriteLine();
			}
			str.Reset();

			parser::AstNode AstRoot = parser::AstBuilder.Build(str);

//			helper::AstDigraphProtter.DrawGraph(AstRoot, "E:\\AstDigraph.dot");
			parser::Optimizer.ShortCutOptimizer(AstRoot);
//			helper::AstDigraphProtter.DrawGraph(AstRoot, "E:\\Optimized.dot");

			process::RootNode ProcessRoot = process::ProcessTreeBuilder.BuildTree(AstRoot);
//			helper::ProcessDigraphProtter.Prot(ProcessRoot, "E:\\ProcessTree.dot");

			root_ = ProcessRoot;

			LiveNotification notifi = new LiveNotification("test", Nico, new TimeSpan(0, 0, 5));
			notifi.LiveProgramStarted+=new EventHandler<LiveProgramEventArgs>(notifi_LiveProgramStarted);
			notifi.Start();

			Console.ReadLine();
			notifi.Dispose();

		}

		private static void BuildProgramList()
		{
			//Prepare LiveProgram list

			LiveNotification notifi = new LiveNotification("Test", Local, new TimeSpan(5000));
			notifi.LiveProgramStarted += new EventHandler<Co9320.NamaAlert.Model.LiveProgramEventArgs>(notifi_LiveProgramStarted);

			ProgramList_ = new List<LiveProgram>();
			notifi.Start();

			Console.ReadLine();
			notifi.Dispose();
			System.Threading.Thread.Sleep(100);
			Console.Clear();
		}

		private static int cnt = 0;

		static void notifi_LiveProgramStarted(object sender, Co9320.NamaAlert.Model.LiveProgramEventArgs e)
		{
			lock (root_)
			{
				if (++cnt % 100 == 0)
				{
					//Console.Clear();
					Console.WriteLine("\tDetect {0} Programs", cnt);
				}
				var normal = normalize::Normalizer.Normalize(e.LiveProgram);
				Write(root_.Execute(normal),e.LiveProgram);
			}
		}

		static void Write(process::ExecuteResult result,LiveProgram program)
		{
			if (result == null || (result.Answer==false && result.ResultElements.Count==0))
			{
				return;
			}

			Console.WriteLine("----------ProcessResult--------");
			Console.WriteLine("lv:{0}", program.Id);
			Console.WriteLine("Title:{0}", program.Title);
			Console.WriteLine("Caster:{0}", program.CasterName);
			Console.WriteLine();
			Console.WriteLine("FinalAnswer:{0}", result.Answer);

			if (result.ResultElements.Count != 0)
			{
				Console.WriteLine("----------DetailReport----------");
				Console.WriteLine();
			}

			for (int i = 0; i < result.ResultElements.Count; i++)
			{
				var cursor=result.ResultElements[i];

				Console.WriteLine("ReportCause:{0}", cursor.ReportCause);

				if (cursor.Source != null)
				{
					Console.WriteLine("----------");

					Console.Write(cursor.Source.Substring(0, cursor.Index));

					if (cursor.ReportCause)
					{
						Console.ForegroundColor = ConsoleColor.Green;
					}
					else
					{
						Console.ForegroundColor = ConsoleColor.Red;
					}

					Console.Write(cursor.Source.Substring(cursor.Index, cursor.Length));
					Console.ResetColor();
					Console.Write(cursor.Source.Substring(cursor.Index + cursor.Length));
					Console.WriteLine();
					Console.WriteLine("----------");
				}

				Console.WriteLine();
			}
			if (result.ResultElements.Count != 0)
			{

				Console.WriteLine();
				Console.WriteLine("----------DetailReportEnd----------");
			}

			Console.WriteLine("----------End of Report----------");
			Console.WriteLine();
//			Console.ReadLine();

		}
	}
}
